#include <iostream>
using namespace std;

#include <cstdio>

const int N = 1e5 + 10;
int n, m, cnt, heap[N];
// 向下调整
void down(int u)
{
    int t = u;
    if (u * 2 <= cnt && heap[u*2] < heap[t])
    {
        t = 2 * u;
    }
    if (u * 2 + 1 <= cnt && heap[u*2+1] < heap[t])
    {
        t = 2 * u + 1;
    }
    if (t != u)
    {
        swap(heap[t], heap[u]);
        down(t);
    }
}

// 向上调整
void up(int u)
{
    while (u / 2 && heap[u / 2] > heap[u])
    {
        swap(heap[u / 2], heap[u]);
        u /= 2;
    }
}

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &heap[i]);
    }

    cnt = n;
    for (int i = n / 2; i > 0; i--)
    {
        down(i);
    }

    while (m--)
    {
        printf("%d ", heap[1]);
        heap[1] = heap[cnt--];
        down(1);
    }
    return 0;
}